import { listCommunity,getCommunity } from "@/api/property/community";
import { listRoom,getRoom } from "@/api/property/room";
import { listUnit ,getUnit} from "@/api/property/unit";
import { listFloor,getFloor } from "@/api/property/floor";

export const roomMixin = {
  data(){
    return {
      //小区数据字典
      communityOptions: [],
      //楼栋数据字典
      floorOptions: [],
      //单元数据字典
      unitOptions: [],
      //房屋数据字典
      roomOptions: [],
    }
  },
  created() {
    this.getCommunityOptionsList();
  },
  methods: {
    //小区列表
    getCommunityOptionsList(){
      listCommunity().then(response => {
        this.communityOptions =response.rows;
      })
    },
    //小区下楼栋列表
    getBuildingData(parentId){
      listFloor({communityId:parentId ||-1}).then(res => {
        this.floorOptions = res.rows
        let index = this.floorOptions.findIndex(x=>x.id == this.form.floorId)
        if(index == -1){
          this.form.floorId = null
          this.form.unitId = null
          this.form.roomId = null
        }
      })
    },
    getUnitList(parentId){
      listUnit({floorId:parentId ||-1}).then(res => {
        this.unitOptions = res.rows
        let index = this.unitOptions.findIndex(x=>x.id == this.form.unitId)
        if(index == -1){
          this.form.unitId = null
          this.form.roomId = null
        }
      })
    },
    //单元下房间列表
    getRoomListData(parentId){
      listRoom({unitId:parentId ||-1}).then(res => {
        this.roomOptions = res.rows
        let index = this.roomOptions.findIndex(x=>x.id == this.form.roomId)
        if(index == -1){
          this.form.roomId = null
        }
      })
    },
    initHouseInfo(currentNode){
      if(currentNode.modelType == 'community'){
        this.form.communityId = currentNode.id
        this.getBuildingData(currentNode.id)
      }else if(currentNode.modelType == 'floor'){
        this.form.floorId = currentNode.id
        getFloor(this.form.floorId).then(res=>{
          this.form.communityId = res.data.communityId
          this.getBuildingData(res.data.communityId)
        })
        this.getUnitList(currentNode.id)
      }else if(currentNode.modelType == 'unit'){
        this.form.unitId = currentNode.id
        this.getRoomListData(currentNode.id)
        getUnit(this.form.unitId).then(res=>{
          this.form.communityId = res.data.communityId
          this.form.floorId = res.data.floorId
          this.getBuildingData(res.data.communityId)
          this.getUnitList(res.data.floorId)
        })
      }else if(currentNode.modelType == 'room'){
        this.form.roomId = currentNode.id
        this.roomIds = [ currentNode.id ]
        getRoom(this.form.roomId).then(res=>{
          this.form.communityId = res.data.communityId
          this.form.unitId = res.data.unitId
          this.form.floorId = res.data.floorId
          this.getBuildingData(res.data.communityId)
          this.getUnitList(res.data.floorId)
          this.getRoomListData(res.data.unitId)
        })
      }
    },
  }

}
